package com.yudi.blog.bussiness.dao;

import com.yudi.blog.bussiness.dto.Blog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @Author YuDi
 * @Date 2021/11/11 11:25
 * @Version 1.0
 */
public interface BlogRepository extends JpaRepository<Blog, Long>, JpaSpecificationExecutor<Blog> {

    @Query("select b from Blog b where b.recommend = true and b.published = true")
    List<Blog> findTop(Pageable pageable);

    @Query("select b from Blog b where b.title like CONCAT('%',?1,'%')")
    Page<Blog> findByQuery(String query, Pageable pageable);

    @Query("select function('date_format',b.updateTime,'%Y') as year from Blog b group by function('date_format',b.updateTime,'%Y') order by year desc ")
    List<String> findGroupYear();

    @Query("select b from Blog  b where function('date_format', b.updateTime, '%Y') = ?1 order by b.updateTime desc ")
    List<Blog> findByYear(String year);


    @Modifying
    @Query("update Blog b set b.views = b.views + 1 where b.id = ?1")
    int updateView(Long id);
}

